define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity.order/index' + location.search,
                    add_url: 'activity.order/add',
                    edit_url: 'activity.order/edit',
                    del_url: 'activity.order/del',
                    multi_url: 'activity.order/multi',
                    import_url: 'activity.order/import',
                    table: 'activity_order',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='activity_id']", form).addClass("selectpage")
                    .data("source", "activity/index")
                    .data("primaryKey", "id").data("field", "title").data("orderBy", "id asc");

                $("input[name='shop_id']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id").data("field", "nickname").data("orderBy", "id asc");

                $("input[name='buy_id']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id").data("field", "nickname").data("orderBy", "id asc");

                $("input[name='dre_id']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id").data("field", "nickname").data("orderBy", "id asc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},

                        {field: 'activity_id', title: __('Activity_id'), visible: false},
                        {field: 'activity.title', title: __('Activity_id'), operate: false},

                        {field: 'shop_id', title: __('Shop_id'), visible: false},
                        {field: 'shop.nickname', title: __('Shop_id'), operate: false},

                        {field: 'buy_id', title: __('Buy_id'), visible: false},
                        {field: 'user.nickname', title: __('Buy_id'), operate: false},

                        {field: 'dre_id', title: __('Dre_id'), visible: false},
                        {field: 'dre.nickname', title: __('Dre_id'), operate: false},

                        {field: 'take_over_way', title: __('Take_over_way'), searchList: {"0": '未选择',"1": '邮寄',"2": "自取"}, formatter: Controller.api.formatter.status},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Controller.api.formatter.status},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'pay_status', title: __('Pay_status'), searchList: {"1":__('Pay_status 1'),"2":__('Pay_status 2'),"3":__('Pay_status 3')}, formatter: Table.api.formatter.status},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'note', title: __('Note'), operate: false, formatter: Controller.api.formatter.showdash},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                showdash: function (value, row, index) {
                    if (value) {
                        return value;
                    }
                    return '-';
                },
                nullorautocontent: function (value, row, index) {
                    if (value) {
                        var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
                        var hover = this.hover != undefined && this.hover ? "autocontent-hover" : "";
                        return "<div class='autocontent-item " + hover + "' style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
                    }
                    return '-';
                },
                status: function (value, row, index) {
                    var colorArr = ["primary", "yellow", "success", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
                    var custom = {};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
                    value = value == null || value.length === 0 ? '' : value.toString();
                    var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                    var index = keys.indexOf(value);
                    var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
                    var display = index > -1 ? this.searchList[value] : null;
                    var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                    if (!color) {
                        color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
                    }
                    if (!display) {
                        display = __(value.charAt(0).toUpperCase() + value.slice(1));
                    }
                    var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                    if (this.operate != false) {
                        html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                    }
                    return html;
                },
            }
        }
    };
    return Controller;
});
